package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class LoginServlet  extends HttpServlet{

    private ServletConfig config;
    private static int counter = 0;

    public LoginServlet() {
        super();
        System.out.println("LoginServlet: " + ++counter + " instances");
    }

    //初始化servlet
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        System.out.println("LoginServlet: invoke init() " + new Date().toString());
    }

    //参考：https://docs.oracle.com/cd/E17802_01/products/products/servlet/2.1/api/javax.servlet.http.HttpServlet.html#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    //Overriding the doGet method automatically also provides support for the HEAD and conditional GET operations.
    //get方法应该只用于请求数据操作，不用于修改数据。
    //已经报告了严重的安全漏洞，因为基于网络的应用程序使用GET不恰当

    //HEAD方法相当于去掉response body部分的GTE方法
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException{
        resp.setContentType("text/html");
        PrintWriter pw = resp.getWriter();
        resp.sendRedirect("index.jsp");
    }
    //Implementations supporting the GET request should override this method to provide an accurate object modification time.
    // This makes browser and proxy caches work more effectively, reducing the load on server and network resources.
    @Override
    protected long getLastModified(HttpServletRequest req){
        return System.currentTimeMillis();
    }
}
